import BaseController from "../../cfw/mvc/BaseController";
import SnowManManager from "./SnowManManager";
import ModuleManager from "../../cfw/module/ModuleManager";
import { UIIndex, ItemState } from "../../cfw/tools/Define";
import HouseController from "../decoration/HouseController";
import SnowToyModel from "./model/SnowToyModel";
import User from "../db/User";
import SDKManager from "../../sdk/SDKManager";
import { ResultState } from "../../sdk/SDKConfig";
import { GameText } from "../../public/Config";
import TipController from "../tip/TipController";


export default class SnowManController extends BaseController {

    private static ins: SnowManController;

    static instance() {
        if (!this.ins) {
            this.ins = new SnowManController()
        }
        return this.ins;
    }

    intoLayer() {
        this.pushView('prefabs/SnowRoomView', "SnowRoomView", SnowManManager.instance(), ModuleManager.getLoader(), UIIndex.ROOT)
    }

    intoSnowBodyRoom() {
        this.pushView('prefabs/SnowBodyRoomView', "SnowBodyRoomView", SnowManManager.instance(), ModuleManager.getLoader(), UIIndex.ROOT)
    }

    back() {
        HouseController.instance().intoLayer()
    }

    next() {
        this.intoSnowBodyRoom();
    }

    onButtonIconClick(model: SnowToyModel) {
        if (!model.isOpen()) {
            TipController.instance().showToast(GameText.ITEM_TOAST, { num: model.getOpenLevel() })
            return;
        }
        if (model.needBuy()) {
            SDKManager.getChannel().showRewardAd(0, (s) => {
                if (s == ResultState.YES) {
                    model.setState(ItemState.GOT)
                }
            })
        } else {
            User.instance().setSnowBodyTypeData(model.getType() - 1, model.getID())
        }

    }

    openItem(model: SnowToyModel) {
        SDKManager.getChannel().showRewardAd(0, (state: ResultState) => {
            cc.log('SnowManController showRewardAd  state == ', state)
            if (state == ResultState.YES) {
                model.setState(ItemState.GOT)
            }
        })
    }
}
